﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;

using System.Linq;

using NHibernate;
using NHibernate.Criterion;

using ProjectBase.Core;
using ProjectBase.Data;
using ProjectBase.Data.NH;

using $prjsn$.Common.Data.Services;
using $prjsn$.Entity.Services;

namespace $safeprojectname$.Services
{
    public class TranslatorDao : BaseDao<Translator, int, TranslatorFilter>, ITranslatorDao
    {
        /// <summary>
        ///     String indicating the path Phrases.Localization
        /// </summary>
        const string Localization = "Phrases.Localization";

        #region ITranslatorDao Members
        /// <summary>
        ///     Evaluates all filters and order-by clauses
        ///     and returns sorted list of matching Translator entities
        /// </summary>
        /// <param name = "filter">ISearchObject&lt;Translator&gt; containing TranslatorFilter not-null instance</param>
        /// <returns>List of Translator entities matching the filtering criteria</returns>
        public override IEnumerable<Translator> Find(TranslatorFilter filter)
        {
            Criteria.Append(Apply(filter));
            
            var result = GetListByCriteria(filter);

            if (filter.OrderHelper.IsNotEmpty())
            {
                filter.OrderBy = filter.OrderHelper;
                filter.OrderHelper = null;
            }
            return result;
        }

        protected override IList<ICriterion> Apply(TranslatorFilter filter)
        {
            var restrictions = base.Apply(filter);

            restrictions.AddCompare(filter, f => f.IdExact); // Searches for exact ID
            restrictions.AddIn(filter, f => f.IDColl);

            restrictions.AddEqual(() => Entity.Key, filter.KeySearch); // even 'string.Empty' is used for WHERE clause
            restrictions.AddEqual(() => Entity.Area, filter.AreaSearch); // even 'string.Empty' is used for WHERE clause

            restrictions.AddLike(filter, f => f.Key); // only not empty nor null is used for WHERE clause
            restrictions.AddLike(filter, f => f.Area); // only not empty nor null is used for WHERE clause


            IList<ICriterion> phraseRestriction = new List<ICriterion>();

            if (filter.SearchedPhrase.IsNotEmpty()) // filter for phrase?
            {
                phraseRestriction.AddLike(() => Entity.Phrases[0].Localization, filter.SearchedPhrase);
                // filtered language
                phraseRestriction.AddLike(() => Entity.Phrases[0].Language, filter.SearchedLanguageName);
            }

            filter.OrderHelper = null;

            var orderByPhrase = string.Empty;
            var orderByValue = true;
            if (filter.OrderBy.IsNotEmpty()) // if there is OrderBy
            {
                // let's try to provide
                var orderBy = filter.OrderBy.First(); // order by on phrases
                if (orderBy.Key.Contains('.')) // if "Phrases.cs"  or "Phrases.en-GB"
                {
                    if (phraseRestriction.IsNull()) // if language filter was not added
                    {
                        // the ordered language is used
                        var language = orderBy.Key.Split('.')[1];
                        // else multi records will be returned
                        phraseRestriction.AddLike(() => Entity.Phrases[0].Language, language);

                    } // new order
                    orderByPhrase = orderBy.Key;
                    orderByValue = orderBy.Value;
                    filter.OrderBy = new Dictionary<string, bool> { { Localization, orderByValue } };

                } // result will be filtered by asked language
            } // and sorted by phrases in that language


            if (phraseRestriction.IsNotEmpty())
            {
                var phraseCriteria = Criteria.CreateSubCriteria(() => Entity.Phrases);
                phraseCriteria.Append(phraseRestriction);
            }

            if (orderByPhrase.IsNotEmpty())
            {
                filter.OrderHelper = new Dictionary<string, bool> { { orderByPhrase, orderByValue } };
            }

            return restrictions;
        }

        public virtual IEnumerable<Translator> GetAll()
        {
            var list = Session.CreateCriteria<Translator>().List<Translator>();
            return list;
        }
        #endregion
    }
}